package com.qdd.freight.controller;

import com.qdd.freight.entity.Datas;
import com.qdd.freight.entity.Result;
import com.qdd.freight.exception.FreightCountFailureException;
import com.qdd.freight.exception.FreightToBeZeroExcption;
import com.qdd.freight.service.FreightService;
import com.qdd.freight.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DtoHandlerController
 * @Description TODO
 * @Author yanpeihao
 * @Date 2019/12/7 8:35
 * @Version 1.0
 **/

@RestController
@RequestMapping("/freight")
public class FreightCountController {

    @Autowired
    private FreightService freightService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * todo 需要改成返回Result对象
     * * @param modeId 快运类型
     * * @param goodsId 商品id
     * * @param entId 仓库id
     * * @param quantity 采购数量
     * * @param company 企业编号
     * * @param consignee 省市区编号
     */
    @RequestMapping("/{modeId}/{goodsId}/{entId}/{quantity}/{company}/{consignee}/{boxVolume}/{singleVolume}/{boxWeight}/{singleWeight}/{QTYRate}")
    public Result orderHandle(@PathVariable Integer modeId, @PathVariable Integer goodsId,
                              @PathVariable Integer entId, @PathVariable Integer quantity,
                              @PathVariable Integer company, @PathVariable String consignee,
                              @PathVariable Double boxVolume, @PathVariable Double singleVolume,
                              @PathVariable Double boxWeight, @PathVariable Double singleWeight,
                              @PathVariable Integer QTYRate) {
        long begin = System.currentTimeMillis();
        System.out.println("测试开始。。。。。。。。。。。。。" + begin);
        /**
         /**
         * 关键属性值，避免使用时再查询数据库中的字段
         */
        System.out.println("============接收到参数打印==============");
        System.out.println("快运编号：yf_mode:" + modeId);
        System.out.println("商品编号：goodsId_id:" + goodsId);
        System.out.println("仓库编号：entId_id:" + entId);
        System.out.println("采购数量：quantity:" + quantity);
        System.out.println("企业编号：company:" + company);
        System.out.println("省市区：consignee_id:" + consignee);
        System.out.println("整箱体积:" + boxVolume);
        System.out.println("单件体积:" + singleVolume);
        System.out.println("整箱重量:" + boxWeight);
        System.out.println("单件重量:" + singleVolume);
        System.out.println("箱规转换:" + QTYRate);

        try {
            //调用service方法，执行计算
            Datas datas = freightService.handle(modeId, goodsId, entId, quantity, company, consignee, boxVolume,
                    singleVolume, boxWeight, singleWeight, QTYRate);
            System.out.println("测试结束。。。。。。。。。。。。。" + (System.currentTimeMillis() - begin));
            return Result.success(datas);
        } catch (FreightCountFailureException be) {
            be.printStackTrace();
            return Result.badRequest(be.getMessage(), new Datas());
        } catch (FreightToBeZeroExcption f) {
            f.printStackTrace();
            return Result.customMsg(HttpStatus.EXPECTATION_FAILED.value(), f.getMessage(), new Datas());
        } catch (Exception e) {
            //计算过程中出现的其它异常
            e.printStackTrace();
            return Result.error("服务器异常，请联系管理员！", new Datas());
        }
    }

}
